package com.tkjt.pms.common.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.List;

import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueAdjust;
import com.tkjt.pms.common.vos.TkIndexValueForScoreVO;
import org.apache.commons.lang.StringUtils;

public class TkGradeUtil {
    public static Double createGrade(TkIndexValueForScoreVO TkIndexValue, String month) {
        Double grade = null;
        switch (month) {
            case "01":
                grade = Double.valueOf(TkIndexValue.getM01());
                break;
            case "02":
                grade = Double.valueOf(TkIndexValue.getM02());
                break;
            case "03":
                grade = Double.valueOf(TkIndexValue.getM03());
                break;
            case "04":
                grade = Double.valueOf(TkIndexValue.getM04());
                break;
            case "05":
                grade = Double.valueOf(TkIndexValue.getM05());
                break;
            case "06":
                grade = Double.valueOf(TkIndexValue.getM06());
                break;
            case "07":
                grade = Double.valueOf(TkIndexValue.getM07());
                break;
            case "08":
                grade = Double.valueOf(TkIndexValue.getM08());
                break;
            case "09":
                grade = Double.valueOf(TkIndexValue.getM09());
                break;
            case "10":
                grade = Double.valueOf(TkIndexValue.getM10());
                break;
            case "11":
                grade = Double.valueOf(TkIndexValue.getM11());
                break;
            case "12":
                grade = Double.valueOf(TkIndexValue.getM12());
                break;
        }
        return grade;
    }

    public static Double createGrade(TkIndexValue tkIndexValue, String month) {
        Double grade = null;
        switch (month) {
            case "01":
                grade = tkIndexValue.getM01();
                break;
            case "02":
                grade = tkIndexValue.getM02();
                break;
            case "03":
                grade = tkIndexValue.getM03();
                break;
            case "04":
                grade = tkIndexValue.getM04();
                break;
            case "05":
                grade = tkIndexValue.getM05();
                break;
            case "06":
                grade = tkIndexValue.getM06();
                break;
            case "07":
                grade = tkIndexValue.getM07();
                break;
            case "08":
                grade = tkIndexValue.getM08();
                break;
            case "09":
                grade = tkIndexValue.getM09();
                break;
            case "10":
                grade = tkIndexValue.getM10();
                break;
            case "11":
                grade = tkIndexValue.getM11();
                break;
            case "12":
                grade = tkIndexValue.getM12();
                break;
        }
        return grade;
    }

    public static void makeChangeAdjustData(TkIndexValue t,TkIndexValue tkIndexValueEntity,List<TkIndexValueAdjust> adjusts,String year,String dataType){
        TkIndexValueAdjust indexValueAdjustEntity = new TkIndexValueAdjust();
        indexValueAdjustEntity.setIndexDepart(t.getIndexDepart());
        indexValueAdjustEntity.setIndexValueCode(t.getIndexValueCode());
        indexValueAdjustEntity.setYear(year);
        indexValueAdjustEntity.setIndexScorer(t.getIndexScorer());
        indexValueAdjustEntity.setDataType(dataType);
        if(tkIndexValueEntity.getMTotal() != null && !String.valueOf(DepartCode.formateForDouble(t.getMTotal())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getMTotal())))){
            indexValueAdjustEntity.setWhich("mTotal");
            indexValueAdjustEntity.setValue(t.getMTotal());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM01() != null && !String.valueOf(DepartCode.formateForDouble(t.getM01())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM01())))){
            indexValueAdjustEntity.setWhich("m01");
            indexValueAdjustEntity.setValue(t.getM01());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM02() != null && !String.valueOf(DepartCode.formateForDouble(t.getM02())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM02())))){
            indexValueAdjustEntity.setWhich("m02");
            indexValueAdjustEntity.setValue(t.getM02());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM03() != null && !String.valueOf(DepartCode.formateForDouble(t.getM03())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM03())))){
            indexValueAdjustEntity.setWhich("m03");
            indexValueAdjustEntity.setValue(t.getM03());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM04() != null && !String.valueOf(DepartCode.formateForDouble(t.getM04())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM04())))){
            indexValueAdjustEntity.setWhich("m04");
            indexValueAdjustEntity.setValue(t.getM04());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM05() != null && !String.valueOf(DepartCode.formateForDouble(t.getM05())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM05())))){
            indexValueAdjustEntity.setWhich("m05");
            indexValueAdjustEntity.setValue(t.getM05());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM06() != null && !String.valueOf(DepartCode.formateForDouble(t.getM06())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM06())))){
            indexValueAdjustEntity.setWhich("m06");
            indexValueAdjustEntity.setValue(t.getM06());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM07() != null && !String.valueOf(DepartCode.formateForDouble(t.getM07())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM07())))){
            indexValueAdjustEntity.setWhich("m07");
            indexValueAdjustEntity.setValue(t.getM07());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM08() != null && !String.valueOf(DepartCode.formateForDouble(t.getM08())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM08())))){
            indexValueAdjustEntity.setWhich("m08");
            indexValueAdjustEntity.setValue(t.getM08());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM09() != null && !String.valueOf(DepartCode.formateForDouble(t.getM09())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM09())))){
            indexValueAdjustEntity.setWhich("m09");
            indexValueAdjustEntity.setValue(t.getM09());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM10() != null && !String.valueOf(DepartCode.formateForDouble(t.getM10())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM10())))){
            indexValueAdjustEntity.setWhich("m10");
            indexValueAdjustEntity.setValue(t.getM10());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM11() != null && !String.valueOf(DepartCode.formateForDouble(t.getM11())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM11())))){
            indexValueAdjustEntity.setWhich("m11");
            indexValueAdjustEntity.setValue(t.getM11());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getM12() != null && !String.valueOf(DepartCode.formateForDouble(t.getM12())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getM12())))){
            indexValueAdjustEntity.setWhich("m12");
            indexValueAdjustEntity.setValue(t.getM12());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getMTotal() != null && !String.valueOf(DepartCode.formateForDouble(t.getMTotal())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getMTotal())))){
            indexValueAdjustEntity.setWhich("mTotal");
            indexValueAdjustEntity.setValue(t.getMTotal());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getQ01() != null && !String.valueOf(DepartCode.formateForDouble(t.getQ01())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getQ01())))){
            indexValueAdjustEntity.setWhich("q01");
            indexValueAdjustEntity.setValue(t.getQ01());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getQ02() != null && !String.valueOf(DepartCode.formateForDouble(t.getQ02())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getQ02())))){
            indexValueAdjustEntity.setWhich("q02");
            indexValueAdjustEntity.setValue(t.getQ02());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getQ03() != null && !String.valueOf(DepartCode.formateForDouble(t.getQ03())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getQ03())))){
            indexValueAdjustEntity.setWhich("q03");
            indexValueAdjustEntity.setValue(t.getQ03());
            adjusts.add(indexValueAdjustEntity);
        }
        if(tkIndexValueEntity.getQ04() != null && !String.valueOf(DepartCode.formateForDouble(t.getQ04())).equals(String.valueOf(DepartCode.formateForDouble(tkIndexValueEntity.getQ04())))){
            indexValueAdjustEntity.setWhich("q04");
            indexValueAdjustEntity.setValue(t.getQ04());
            adjusts.add(indexValueAdjustEntity);
        }
    }

    public static Double changeFormate(String data) {
        if(data != null){
            if(data.contains("%")) {
                String doubleString = data.substring(0,data.indexOf("%"));
                double doubleData = Double.valueOf(doubleString);
                BigDecimal b1 = new BigDecimal(Double.toString(doubleData));
                BigDecimal b2 = new BigDecimal(Double.toString(100));
                return b1.divide(b2,5, RoundingMode.HALF_UP).doubleValue();
            }else {
                return Double.valueOf(data);
            }
        }else{
            return null;
        }

    }

    public static Double updateGrade(TkIndexValue TkIndexValue,Double grade,String month) {
        if(grade == null){
            grade = 0d;
        }
        switch (month) {
            case "01":
                TkIndexValue.setM01(grade);
                break;
            case "02":
                TkIndexValue.setM02(grade);
                break;
            case "03":
                TkIndexValue.setM03(grade);
                break;
            case "04":
                TkIndexValue.setM04(grade);
                break;
            case "05":
                TkIndexValue.setM05(grade);
                break;
            case "06":
                TkIndexValue.setM06(grade);
                break;
            case "07":
                TkIndexValue.setM07(grade);
                break;
            case "08":
                TkIndexValue.setM08(grade);
                break;
            case "09":
                TkIndexValue.setM09(grade);
                break;
            case "10":
                TkIndexValue.setM10(grade);
                break;
            case "11":
                TkIndexValue.setM11(grade);
                break;
            case "12":
                TkIndexValue.setM12(grade);
                break;
        }
        return grade;
    }

    public static double getGradeByMonth(String monStr,TkIndexValue entity) {
        double result = 0.0D;
        switch (monStr) {
            case "01":
                result = entity.getM01();
                break;
            case "02":
                result = entity.getM02();
                break;
            case "03":
                result = entity.getM03();
                break;
            case "04":
                result = entity.getM04();
                break;
            case "05":
                result = entity.getM05();
                break;
            case "06":
                result = entity.getM06();
                break;
            case "07":
                result = entity.getM07();
                break;
            case "08":
                result = entity.getM08();
                break;
            case "09":
                result = entity.getM09();
                break;
            case "10":
                result = entity.getM10();
                break;
            case "11":
                result = entity.getM11();
                break;
            case "12":
                result = entity.getM12();
                break;
            default:
                break;
        }
        return result;
    }
}
